<?php
/**
 * to show this slide, must do below steps:
 * 1)add booking with court name like "like '%重庆天地%'",
 * 2)first show courts in night, so must have courts during the period
 * 3)second show courts in weekend, so must have courts in weekends
 */
$treeTable = new BTreeTable('BWInputValue');
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$wxControl = $screen->getChildControl('BUIWXControl_1');
$app = Blyn::app()->getActiveApp();

//足球天地＋晚上＋7人球场
$condition1 = array(
    'city' => 'china.chongqing',
    'bookingStatus' => 'available',
    //下面的两个条件非常重要：
    'bookingStartTime' => '<=' . time(),
    'bookingEndTime' => '>=' . time(),
    'startOfTimeSlot' => '>=170000', //下午5点之后
    'endOfTimeSlot' => '<=240000',
    'courtDisplayName' => "like '%重庆天地%'",
    //only get item for current space
    'spaceId' => Blyn::app()->getActiveSpace()->getId()
);
$condition1 = 'city=china.chongqing bookingStartTime<=' . time() . ' bookingEndTime>=' . time();
$condition1 .= ' startOfTimeSlot<=240000 endOfTimeSlot>=170000';
$condition1.= " courtDisplayName=重庆天地";
$condition1.= " courtType=soccer_court_7";
$condition1.= ' spaceId=' . Blyn::app()->getActiveSpace()->getId();
$count1 = NULL;
$count2 = NULL;
$count1 = Blyn::app()->getActiveApp()->findCountByFindText($condition1);
//$count1 = $treeTable->getCount(array('itemData' => $condition1));
//$ajaxLink1 = $control->getAjaxScript('showAvailableBookingList', array('findContext' => $condition1));
//$courtDisplayName = '重庆天地11号足球公园';
//$courtAddress = "重庆化龙桥";
//$regionAlias = '重庆';
//$list1 = $treeTable->getList(array('itemData' => $condition1));
$view1 = $app->findByFindText($condition1);
$list1 = reset($view1);
if ($list1 != NULL && $list1 instanceof BTreeList) {
    $courtDisplayName = $list1->getItem('courtDisplayName')->getValue();
    //read image
    $str = $list1->getItem('courtName')->value;
    $str = trim($str);
    if (!empty($str)) {
        $result = '';
        for ($i = 0; $i < strlen($str); $i++) {
            if (is_numeric($str[$i])) {
                $result.=$str[$i];
            }
        }
    }
    $bookdetail = new BWMessage($result);
    $courtMedia = $bookdetail->getMediaManager()->getMedia('image');
    $courtImage = " <h4>No Picture!</h4>";
    if ($courtMedia instanceof BWMedia) {
        $courtImage = $bookdetail->getMediaManager()->getMedia('image')->getContent();
    }
    $courtAddress = $list1->getItem('courtAddress')->getValue();
    $regionAlias = $list1->getItem('regionAlias')->getValue();
    $bookableId1 = $list1->getItem('bookableId')->getValue();
    $link1 = $wxControl->getLinkUrl(array(
        'name' => '晚间可预定场地',
        'id' => 'booking_choose_time'), array(
        //'listPage' => 'booking_list_1',
        'bookableId' => $bookableId1,
        'findText' => $condition1,
        'flagText' => '晚间可预定场地',
        'nowCity' => 'china.chongqing',
        'backPage' => 'booking_find_1'
    ));
}
//$regionAlias = '重庆';
//足球天地＋周末＋7人球场
$condition2 = array(
    'city' => 'china.chongqing',
    'bookingStatus' => 'available',
    //下面的两个条件非常重要：
    'bookingStartTime' => '<=' . time(),
    'bookingEndTime' => '>=' . time(),
    'startDay' => array('6', '7'),
    'courtDisplayName' => "like '%重庆天地%'",
    //only get item for current space
    'spaceId' => Blyn::app()->getActiveSpace()->getId()
);
$condition2 = 'city=china.chongqing bookingStartTime<=' . time() . ' bookingEndTime>=' . time();
$condition2 .= ' startDay=6 startDay=7';
$condition2 .= " courtDisplayName=重庆天地";
$condition2 .= " courtType=soccer_court_7";
$condition2 .= ' spaceId=' . Blyn::app()->getActiveSpace()->getId();
//$count2 = $treeTable->getCount(array('itemData' => $condition2));
$count2 = Blyn::app()->getActiveApp()->findCountByFindText($condition2);
//$ajaxLink2 = $control->getAjaxScript('showAvailableBookingList', array('findContext' => $condition2));
//$link2 = $wxControl->getLinkUrl('showAvailableBookingList', array('findContext' => $condition2));
//$list2 = $treeTable->getList(array('itemData' => $condition2));
$view2 = $app->findByFindText($condition2);
$list2 = reset($view2);
if ($list2 != NULL && $list2 instanceof BTreeList) {
    $courtDisplayName2 = $list2->getItem('courtDisplayName')->getValue();
    //read image
    $str2 = $list2->getItem('courtName')->value;
    $str2 = trim($str2);
    if (!empty($str2)) {
        $result2 = '';
        for ($i = 0; $i < strlen($str2); $i++) {
            if (is_numeric($str2[$i])) {
                $result2.=$str2[$i];
            }
        }
    }
    $bookdetail2 = new BWMessage($result2);
    $courtMedia2 = $bookdetail2->getMediaManager()->getMedia('image');
    $courtImage2 = " <h4>No Picture!</h4>";
    if ($courtMedia2 instanceof BWMedia) {
        $courtImage2 = $bookdetail2->getMediaManager()->getMedia('image')->getContent();
    }
    $courtAddress2 = $list2->getItem('courtAddress')->getValue();
    $regionAlias2 = $list2->getItem('regionAlias')->getValue();
    $bookableId2 = $list2->getItem('bookableId')->getValue();
    $link2 = $wxControl->getLinkUrl(array(
        'name' => '周末可预定场地',
        'id' => 'booking_choose_time'), array(
        //'listPage' => 'booking_list_1',
        'bookableId' => $bookableId2,
        'findText' => $condition2,
        'flagText' => '周末可预定场地',
        'nowCity' => 'china.chongqing',
        'backPage' => 'booking_find_1'
    ));
}
//$count2 = 21;
if ($count1 != NULL || $count2 !== NULL) {
    ?>
    <div id="imgGo">
        <ul id="testUl">
            <?php if ($count1 > 0) { ?>
                <li>
                    <div class="main">
                        <a href="<?php echo $link1; ?>">
                            <div class="tu"><img style="width:100%;height:100%;" src="<?php echo Yii::app()->request->baseUrl . $courtImage; ?>"></div>
                            <div class="wen">
                                <p style="color:#8CC685;font-weight:bold;overflow: hidden;text-overflow: ellipsis;width: 200px;height: 28px;white-space: nowrap;"><?php echo $courtDisplayName; ?></p>
                                <p style="color:#8CC685;overflow: hidden;text-overflow: ellipsis;width: 200px;height: 28px;white-space: nowrap;"><?php echo $regionAlias;     ?><span style="color:#B3B1B2"><?php echo $courtAddress; ?></span></p>
                                <p style="color:#8D8D8D"><span style="color:#EB6307">晚上</span>&nbsp&nbsp&nbsp剩余:&nbsp&nbsp<span style="font-weight:bold;color:#ED7219;font-size:30px">
                                        <?php echo $count1; ?>
                                    </span>
                                </p>
                            </div>
                        </a>
                        <div style="width:100%;height:0;clear:both"></div>
                    </div>
                </li>
            <?php } ?>
            <?php if ($count2 > 0) { ?>
                <li>
                    <div class="main">
                        <a href="<?php echo $link2; ?>">
                            <div class="tu"><img style="width:100%;height:100%;" src="<?php echo Yii::app()->request->baseUrl . $courtImage2; ?>"></div>
                            <div class="wen">
                                <p style="color:#8CC685;font-weight:bold;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;"><?php echo $courtDisplayName2; ?></p>
                                <p style="color:#8CC685;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;"><?php echo $regionAlias2; ?><span style="color:#B3B1B2;"><?php echo $courtAddress2; ?></span></p>
                                <p style="color:#8D8D8D"><span style="color:#EB6307">周末</span>&nbsp&nbsp&nbsp剩余:&nbsp&nbsp<span style="font-weight:bold;color:#ED7219;font-size:30px">
                                        <?php echo $count2; ?>
                                    </span>
                                </p>
                            </div>
                        </a>
                        <div style="width:100%;height:0;clear:both"></div>
                    </div>
                </li>
            <?php } ?>
        </ul>
        <div id="imgIndex"></div>
    </div>
<?php } ?>